package com.rc.saas.mini.hlhtcec2.config;

import com.rc.saas.mini.common.utils.EncodeUtil;
import com.rc.saas.mini.common.utils.HMacMD5;
import com.rc.saas.mini.model.hlhtsk.HlhtskBizOperator;
import com.rc.saas.mini.model.hlhtsk.HlhtskBizTenant;
import com.rc.saas.mini.vo.cec.StdRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 中电联类互联互通
 * 公共签名和加密工具类
 */
public class HlhtBizPubFetchTool {
    private static Logger logger = LogManager.getLogger(HlhtBizPubFetchTool.class);

    /**
     * 请求方
     */
    public static String genDDDecodeData(HlhtskBizTenant config, String jsonData) {
        return EncodeUtil.decrypt(jsonData, getAESSecret(config), getAESLV(config));
    }

    /**
     * 响应方
     */
    public static String genDDDecodeData2(HlhtskBizOperator config, String jsonData) {
        return EncodeUtil.decrypt(jsonData, getAESSecret2(config), getAESLV2(config));
    }

    public static String genDDEncodeData(HlhtskBizTenant config, String jsonData) {
        return EncodeUtil.encrypt(jsonData, getAESSecret(config), getAESLV(config));
    }

    public static String genDDEncodeData2(HlhtskBizOperator config, String jsonData) {
        //logger.info("数据加密,DataSecret:{},DataSecretIV:{}", config.getDataSecret(), config.getDataSecretiv());
        return EncodeUtil.encrypt(jsonData, getAESSecret2(config), getAESLV2(config));
    }

    public static String genDDSig(HlhtskBizTenant config, String enString, String timeStamp) {
        //我请求你们的，用的是你们的加密参数，但OperatorID是请求方的
        return HMacMD5.getHmacMd5Str(getSigSecret(config), config.getHlhtOperatorId() + enString + timeStamp + "0001");
    }

    public static String genDDSig2(HlhtskBizOperator config, String enString, String timeStamp) {
        //logger.info("数据签名,OperatorID:{},SigSecret:{},enString:{}", config.getTenantCode(), config.getSigSecret(), enString);
        //我请求你们的，用的是你们的加密参数，但OperatorID是请求方的

        if (StringUtils.isBlank(config.getLicenseTenantCode())) {
            return HMacMD5.getHmacMd5Str(getSigSecret2(config), config.getTenantCode() + enString + timeStamp + "0001");
        } else {
            return HMacMD5.getHmacMd5Str(getSigSecret2(config), config.getLicenseTenantCode() + enString + timeStamp + "0001");
        }
//        return HMacMD5.getHmacMd5Str(getSigSecret2(config), config.getTenantCode() + enString + timeStamp + "0001");
    }

    public static String getAESSecret(HlhtskBizTenant config) {
        return config.getDataSecret();
    }

    public static String getAESSecret2(HlhtskBizOperator config) {
        return config.getDataSecret();
    }

    public static String getAESLV(HlhtskBizTenant config) {
        return config.getDataSecretiv();
    }

    public static String getAESLV2(HlhtskBizOperator config) {
        return config.getDataSecretiv();
    }

    //public static String getOperatorSecret(KyConfig config) {
    //    Operator operator = getOperator(operatorId)
    //    return operator.getOperatorSecret();
    //}

    public static String getSigSecret(HlhtskBizTenant config) {
        return config.getSigSecret();
    }

    public static String getSigSecret2(HlhtskBizOperator config) {
        return config.getSigSecret();
    }

    public static boolean checkSig(HlhtskBizTenant config, String hlhtOperatorId, StdRequest request) {
        String Data = request.getData();
        String TimeStamp = request.getTimeStamp();
        String Seq = request.getSeq();
        String Sig = request.getSig();

        String genSig = HMacMD5.getHmacMd5Str(getSigSecret(config), hlhtOperatorId + Data + TimeStamp + Seq);
        if (Sig.equals(genSig)) {
            return true;
        }
        return false;
    }

    public static boolean checkSig2(HlhtskBizOperator config, StdRequest request) {
        String Data = request.getData();
        String TimeStamp = request.getTimeStamp();
        String Seq = request.getSeq();
        String Sig = request.getSig();

        String genSig = HMacMD5.getHmacMd5Str(getSigSecret2(config), config.getTenantCode() + Data + TimeStamp + Seq);
        if (Sig.equals(genSig)) {
            return true;
        }
        return false;
    }

    //查库里的operator表进行真正处理
    //private static Operator getOperator() {
    //    Operator operator = new Operator();
    //    return operator;
    //}

    //private static Operator getOperator2(String operatorId) {
    //    Operator operator = new Operator();
    //    return operator;
    //}
}
